// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import { actionRouter } from './router';
let flag = false;
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
    if (store.state.user_name) {
        // if (flag) {
        //     next()
        // } else {
        //     flag = true;
        //     router.addRoutes(actionRouter);
        //     next(to)
        //     console.log(router);
        // }
        if (store.state.newRouters.length !== 0) {
            next();
        } else {
            let newRouters;
            if (store.state.role == "A") {
                newRouters = actionRouter;
            } else {
                let children = actionRouter[0].children.filter(route => {
                    if (route.meta) {
                        if (route.meta.role == store.state.role) {
                            return true
                        } else {
                            return false
                        }
                    } else {
                        return true
                    }
                });
                newRouters = actionRouter;
                newRouters[0].children = children;
            }
            router.addRoutes(newRouters);
            store.dispatch("setRoles", newRouters);
            next(to)
        }

    } else {
        to.path.includes("login") ? next() : next("/login");
    }

})


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})